 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.raitcon.hibernate.backbean;

import com.raitcon.hibernate.bean.Area;
import com.raitcon.hibernate.bean.Asignatura;
import com.raitcon.hibernate.db.AreaDB;
import com.raitcon.hibernate.db.AsignaturaDB;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import org.apache.log4j.Logger;

/**
 *
 * @author Mike
 */
@ManagedBean(name = "areaBean")
@ViewScoped
public class AreaBean implements Serializable {
    
    
   protected static Logger log = Logger.getLogger(AreaBean.class); 
   private static  final  long serialVersionUID=-2377612760546575078L;
   private String currentItem = "";
   private List<SelectItem> areaList = new ArrayList<SelectItem>();
  
   private List<SelectItem> asignaturas = new ArrayList<SelectItem>();
   private String selectedArea = "0";
   private String selectedAsignatura = "0";
   
   @PostConstruct
   protected void init(){
       log.debug("init... areaBean");
       AreaDB areaDB=new AreaDB();
        List<Area> areaLst=areaDB.getAreaAll();
        SelectItem item = new SelectItem("0", "--Selecciona Area--");
        getAreaList().add(item);
        for(Area a:areaLst){
            item = new SelectItem(a.getIdArea(), a.getDescripcion());
             getAreaList().add(item);
        }
        
        asignaturas.add(new SelectItem(0, "--Selecciona Asignatura--"));
        System.out.println("selectedState::::" + selectedAsignatura);
      
   }

    /**
     * @return the selectedArea
     */
    public String getSelecteArea() {
        return selectedArea;
    }

    /**
     * @param selectedArea the selectedArea to set
     */
    public void setSelectedCiclo(String selectedArea) {
        this.selectedArea = selectedArea;
    }

    /**
     * @return the areaList
     */
    public List<SelectItem> getAreaList() {
        return areaList;
    }

    /**
     * @param areaList the areaList to set
     */
    public void setAreaList(List<SelectItem> areaList) {
        this.areaList = areaList;
    }

    /**
     * @return the asignaturas
     */
    public List<SelectItem> getAsignaturas() {
        return asignaturas;
    }

    /**
     * @param asignaturas the asignaturas to set
     */
    public void setAsignaturas(List<SelectItem> asignaturas) {
        this.asignaturas = asignaturas;
    }

    /**
     * @return the selectedAsignatura
     */
    public String getSelectedAsignatura() {
        return selectedAsignatura;
    }

    /**
     * @param selectedAsignatura the selectedAsignatura to set
     */
    public void setSelectedAsignatura(String selectedAsignatura) {
        this.selectedAsignatura = selectedAsignatura;
    }
    
    
     public void valueChanged(ValueChangeEvent event) {
        asignaturas.clear();
        if (null != event.getNewValue()) {
           // String[] currentItems;
            String codArea=(String)event.getNewValue();
            asignaturas.add(new SelectItem("0", "--Selecciona Asignatura--"));
            AsignaturaDB asignaturaDB=new AsignaturaDB();
            List<Asignatura>asignaturaList=asignaturaDB.getAsignaturaByIdArea(codArea);
 
            for (Asignatura asign:asignaturaList) {
                SelectItem item = new SelectItem(asign.getIdAsignatura(),asign.getDescripcion());
                 asignaturas.add(item);
                 //FacesContext.getCurrentInstance().renderResponse();
            }
            if(asignaturaList==null){
                asignaturas.add(new SelectItem("0", "--Selecciona Asignatura--"));
            }
                
        }
    }
     
    
}
